﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Mandarine.Controls.ApplicationBar
{
    /// <summary>
    /// アプリケーションバー用のイベントトリガークラスを提供します
    /// </summary>
    public class AppBarItemTrigger : TriggerBase<PhoneApplicationPage>
    {
        private IApplicationBarMenuItem _item;

        /// <summary>
        /// アプリケーションバーアイテムのタイプ (ボタン、メニュー)値
        /// </summary>
        public AppBarItemType Type { get; set; }

        /// <summary>
        ///  アプリケーションバーのアイテムを探すのに使用する、このトリガを一意に識別するId値
        /// </summary>
        public string Id { get; set; }


        #region Text dependency property

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(AppBarItemTrigger)
            , new PropertyMetadata((s, e) =>
            {
                var t = (AppBarItemTrigger) s;
                if (t._item != null && !String.IsNullOrWhiteSpace(t.Text))
                {
                    t._item.Text = t.Text;
                }
            }));
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        #endregion
        #region IsEnabled dependency property

        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.Register("IsEnabled", typeof(bool), typeof(AppBarItemTrigger)
            , new PropertyMetadata(true, (s, args) =>
            {
                var t = (AppBarItemTrigger)s;
                if (t._item != null)
                {
                    t._item.IsEnabled = t.IsEnabled;
                }
            }));
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        #endregion

        protected override void OnAttached()
        {
            _item = AssociatedObject.ApplicationBar.FindItem(Type, Id);

            if (_item != null)
            {
                _item.Click += ItemClick;
                _item.IsEnabled = IsEnabled;

                // Do not change the Text of an app bar item if DisplayText is empty,
                // otherwise you'll get an InvalidOperationException/ArgumentNullException.
                if (!String.IsNullOrWhiteSpace(Text))
                {
                    _item.Text = Text;
                }
            }

            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            if (_item != null)
            {
                _item.Click -= ItemClick;
                //_item.Text = ItemText;
            }

            base.OnDetaching();
        }

        /// <summary>
        /// アプリケーションバーアイテムがクリックされた際に内部アクションを実行する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ItemClick(Object sender, EventArgs args)
        {
            InvokeActions(null);
        }

    }
}
